#include "Main/stdafx.h"
#include "FKNNPlayer.h"
#include "FKNNSoundFun.h"
#include "Games/NiuNiuCardSprite.h"
#include "Games/NiuNiuGameLogic.h"

FKNNPlayer::FKNNPlayer(int iIdex,cocos2d::Node* pSeatNode )
	:GamePlayer(NULL)
	,m_iIdex(iIdex)
	, m_userMoney(0)
	,m_pSeatNode(pSeatNode)
	,m_kHandCardCout(0)
	, m_headUrl("")
{
	init();
}

FKNNPlayer::~FKNNPlayer()
{

}

void FKNNPlayer::init()
{
	m_handPanel = (Node*) m_pSeatNode->getChildByName(StringUtils::format("Panel_card%d", m_iIdex));

	m_PushPanel = (Sprite*)m_pSeatNode->getChildByName(StringUtils::format("Sprite_m%d", m_iIdex));
	m_PushPanel->setVisible(false);

	m_uPanel = (Layout*)m_pSeatNode->getChildByName(StringUtils::format("Panel_u%d", m_iIdex));
	m_uPanel->setVisible(false);

	m_head = (Sprite*)m_uPanel->getChildByName("Sprite_head");
	m_offLine_sprite = (Sprite*)m_uPanel->getChildByName("off_line");
	m_zhuang_sprite = (Sprite*)m_uPanel->getChildByName("Sprite_zhuang");
	m_ready_sprite = (Sprite*)m_uPanel->getChildByName("Status_Ready");
	m_userInfo_name = (Text*)m_uPanel->getChildByName("Text_name");
	m_userInfo_money = (Text*)m_uPanel->getChildByName("Text_money");
	m_spriteChairId = (Sprite*)m_uPanel->getChildByName("Sprite_chairId");

	auto cardTip = (ImageView* )m_pSeatNode->getChildByName("Image_cardTip");
	m_cardTip = (ImageView*) cardTip->clone();
	m_uPanel->addChild(m_cardTip);
	m_cardTip->setPosition(m_offLine_sprite->getPosition());
	
	PlayerLeave();
}
int  FKNNPlayer::getIdex()
{
	return m_iIdex;
}
void FKNNPlayer::defaultState()
{
	m_kHandCardCout = 0;

	m_zhuang_sprite->setVisible(false);
	m_ready_sprite->setVisible(false);
	m_offLine_sprite->setVisible(false);
	m_cardTip->setVisible(false);
	m_PushPanel->setVisible(false);

	m_handPanel->removeAllChildrenWithCleanup(true);

	upPlayerState();
}

void FKNNPlayer::startGame()
{
	m_ready_sprite->setVisible(false);
}

void FKNNPlayer::EndGame()
{
	if (m_pUserItem == NULL )
	{
		return;
	}
	m_PushPanel->setVisible(false);
}

void FKNNPlayer::PlayerEnter()
{
	m_uPanel->setVisible(true);
	m_offLine_sprite->setVisible(false);

	TextAtlas* chairIdAtlas = (TextAtlas*)m_spriteChairId->getChildByName("AtlasLabel_num");
	chairIdAtlas->setString(StringUtils::format("%d", GetChairID()));

	std::string updateHeadUrl = StringUtils::format("http://dl.rdaohy.com:8888/app/face/get?uid=%d", GetUserID());
	MCWebReq::instance().sendRequestDocumentUrl(updateHeadUrl, nullptr, CC_CALLBACK_1(FKNNPlayer::Rsp_uptHeadUrl_list, this));
}

void FKNNPlayer::PlayerLeave()
{
	defaultState();
	m_userMoney = 0;
	m_headUrl = "";
	m_uPanel->setVisible(false);
}

bool FKNNPlayer::checkUserSitDown()
{
	return m_uPanel->isVisible();
}

void FKNNPlayer::upPlayerInfo()
{
	m_userInfo_name->setString(GetNickName());
	//m_userInfo_money->setString(StringUtils::format("%lld",m_userMoney));
	
// 	if (GetHeadHttp() != "")
// 	{
// 		ImagicDownManager::Instance().addDown(m_head,
// 			GetHeadHttp(),GetUserID());
// 	}
// 	else
// 	{
// 		if (GetGender() == 0)
// 			m_head->setTexture("common/Head0.png");
// 		else
// 			m_head->setTexture("common/Head1.png");
// 	}
}
void FKNNPlayer::setZhuang()
{
	m_zhuang_sprite->setVisible(true);
}
void FKNNPlayer::showAddGold(int64 iGold)
{
	Text* tMoney = (Text*)m_PushPanel->getChildByName("Text_pushMoney");
	if(tMoney)
		tMoney->setString(StringUtils::format("%lld", iGold));
	m_PushPanel->setVisible(true);
}

void FKNNPlayer::upPlayerState()
{
	if (GetUserStatus() == US_READY)
	{
		m_ready_sprite->setVisible(true);
	}
	if (GetUserStatus() == US_OFFLINE)
	{
		m_offLine_sprite->setVisible(true);
	}
}

void FKNNPlayer::seatDownCard()
{
	for (int i = 0;i<m_handPanel->getChildrenCount();i++)
	{
		cocos2d::Sprite* pSprite = dynamic_cast<cocos2d::Sprite*>(m_handPanel->getChildren().at(i));
		if (pSprite == NULL || pSprite->getTag() != 1)
		{
			continue;
		}
		pSprite->setPositionY(CARD_DWON_POSY);
	}
}

void FKNNPlayer::ClickCard(cocos2d::Node* pCard)
{
	if (pCard == NULL )return;

	int nPosY = pCard->getPositionY();
	if (nPosY == CARD_DWON_POSY)
	{
		pCard->setPositionY(CARD_UP_POSY);
	}
	else
	{
		pCard->setPositionY(CARD_DWON_POSY);
	}
}

void FKNNPlayer::showZhuangcircle()
{
	
}

int FKNNPlayer::getSelectedCardNum()
{
	int initNum = 0;
	for (int i = 0; i < m_handPanel->getChildrenCount(); i++)
	{
		CCardSprite* pSprite = dynamic_cast<CCardSprite*>(m_handPanel->getChildren().at(i));
		if (pSprite == NULL || pSprite->getTag() != 1)
		{
			continue;
		}
		if (pSprite->getStatus() == CCardSprite::ST_SELECTED)
			initNum++;
	}
	return initNum;
}

void FKNNPlayer::showResultScore(int64 resultScore)
{
	updateUserMoney(resultScore);

	Sprite* typeSprite = (Sprite*)m_handPanel->getChildByTag(99);
	if (typeSprite)
	{
		TextAtlas* tAtlas = NULL;
		SCORE retMoney = 0;
		if (resultScore > 0)
		{
			tAtlas = (TextAtlas*)m_pSeatNode->getChildByName("AtlasLabel_win");
			retMoney = resultScore;
		}	
		else
		{
			tAtlas = (TextAtlas*)m_pSeatNode->getChildByName("AtlasLabel_lost");
			retMoney = abs(resultScore);
		}

		TextAtlas* newText = (TextAtlas*)tAtlas->clone();
		newText->setString(StringUtils::format("/%lld", retMoney));
		newText->setScale(0.1f);
		m_handPanel->addChild(newText,100,100);
		newText->setPosition(typeSprite->getPosition());
		newText->runAction(Spawn::createWithTwoActions(MoveBy::create(1.0f,Vec2(0,m_handPanel->getContentSize().height/2 + newText->getContentSize().height/2)), ScaleTo::create(1.0f,1.0f)));
	}
}

void FKNNPlayer::showChatWord(std::string szChatString)
{
	Vec2 endPos = m_uPanel->getPosition();
	Layout* chatPanel = NULL;
	if (m_iIdex > 6)
	{
		chatPanel = (Layout*)m_pSeatNode->getChildByName("Image_chatRight");
		endPos.x -= m_uPanel->getContentSize().width;
		endPos.x -= chatPanel->getContentSize().width / 2;
	}
	else if (m_iIdex > 3)
	{
		chatPanel = (Layout*)m_pSeatNode->getChildByName("Image_chatLeft");
		endPos.y -= m_uPanel->getContentSize().height;
		endPos.y -= chatPanel->getContentSize().height / 2;
	}
	else if (m_iIdex == 0)
	{
		chatPanel = (Layout*)m_pSeatNode->getChildByName("Image_chatLeft");
		endPos.x += m_uPanel->getContentSize().width;
		endPos.x += chatPanel->getContentSize().width / 2;
		endPos.y += m_uPanel->getContentSize().height;
		endPos.y += chatPanel->getContentSize().height / 2;
	}
	else
	{
		chatPanel = (Layout*)m_pSeatNode->getChildByName("Image_chatLeft");
		endPos.x += m_uPanel->getContentSize().width;
		endPos.x += chatPanel->getContentSize().width / 2;
	}
	Layout* newPanel = (Layout*)chatPanel->clone();
	Text* chatText = (Text*)newPanel->getChildByName("Text_value");
	chatText->setString(szChatString);
	m_pSeatNode->addChild(newPanel);
	newPanel->setPosition(endPos);
	newPanel->runAction(Sequence::create(DelayTime::create(2.0f), RemoveSelf::create(), NULL));
}

void FKNNPlayer::showChatFace(word wItemIndex)
{
	Vec2 endPos = m_uPanel->getPosition();
	Sprite* chatPanel = Sprite::create(StringUtils::format("chat/face%d.png", wItemIndex + 1));
	if (m_iIdex > 6)
	{
		endPos.x -= m_uPanel->getContentSize().width;
		endPos.x -= chatPanel->getContentSize().width / 2;
	}
	else if (m_iIdex > 3)
	{
		endPos.y -= m_uPanel->getContentSize().height;
		endPos.y -= chatPanel->getContentSize().height / 2;
	}
	else if (m_iIdex == 0)
	{
		endPos.x += m_uPanel->getContentSize().width;
		endPos.x += chatPanel->getContentSize().width / 2;
		endPos.y += m_uPanel->getContentSize().height;
		endPos.y += chatPanel->getContentSize().height / 2;
	}
	else
	{
		endPos.x += m_uPanel->getContentSize().width;
		endPos.x += chatPanel->getContentSize().width / 2;
	}
	m_pSeatNode->addChild(chatPanel);
	chatPanel->setPosition(endPos);
	chatPanel->runAction(Sequence::create(DelayTime::create(2.0f), RemoveSelf::create(), NULL));
}

void FKNNPlayer::initUserMoney(SCORE money)
{
	m_userMoney = money;
	m_userInfo_money->setString(StringUtils::format("%lld", m_userMoney));
}

void FKNNPlayer::updateUserMoney(SCORE money)
{
	m_userMoney += money;
	m_userInfo_money->setString(StringUtils::format("%lld", m_userMoney));
}

void FKNNPlayer::showCardTipNode(std::string str)
{
	m_cardTip->setVisible(true);
	TextAtlas* tipValue = (TextAtlas*)m_cardTip->getChildByName("AtlasLabel_num");
	tipValue->setString(str);
}

void FKNNPlayer::Rsp_uptHeadUrl_list(const std::string& data)
{
	rapidjson::Document doc;
	doc.Parse<rapidjson::kParseDefaultFlags>(data.c_str());
	if (doc.HasParseError() || !doc.HasMember("face"))
	{
		return;
	}
	std::string strUrl = doc["face"].GetString();
	if (strUrl.find("http") != std::string::npos)
	{
		m_headUrl = strUrl;
		ImagicDownManager::Instance().addDown(m_head,
			strUrl, GetUserID());
	}
	else
	{
		if (GetGender() == 0)
			m_head->setTexture("common/Head0.png");
		else
			m_head->setTexture("common/Head1.png");
	}
}

void FKNNPlayer::showDefaultSound(int itemIndex)
{
	FKNNSoundFun::playEffect(StringUtils::format("default/default%d", itemIndex));
}
